package com.bestv.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by charle-chen on 15/11/18.
 */

public class DbEntity implements Db {

    private String driver = "";
    private String url = "";
    private String userName = "";
    private String password = "";
    private Connection conn = null;
    private Statement st = null;

    public DbEntity() {

    }

    public DbEntity(String driver, String url, String userName, String password) {
        super();
        this.driver = driver;
        this.url = url;
        this.userName = userName;
        this.password = password;
    }

    public Connection getConn() {
        try {
            if (conn == null) {
                Class.forName(driver);
                conn = (Connection) DriverManager.getConnection(url, userName, password);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void closeDb(){
        try{
            if(conn!=null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
